//
//  MSMeTopView.m
//  MS
//
//  Created by He Yuan on 13-2-21.
//  Copyright (c) 2013年 He Yuan. All rights reserved.
//

#import "MSMeTopView.h"
#import "UIButton+WebCache.h"
#import <QuartzCore/QuartzCore.h>
#import "UIColor+ColorUtils.h"

@implementation MSMeTopView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"me_top_bg"]];
        
        self.frame = frame;
                
        CGRect avatarFrame = CGRectMake(10,17,65,65);
        avatarBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [avatarBtn setImage:[UIImage imageNamed:@"default_img@2x.png"] forState:UIControlStateNormal];
        avatarBtn.layer.cornerRadius = 2.0;
        avatarBtn.layer.masksToBounds = YES;
        avatarBtn.layer.borderWidth = 2;
        avatarBtn.layer.borderColor = [UIColor colorWithWhite:0.912 alpha:1.000].CGColor;
        [avatarBtn setFrame:avatarFrame];
        [self addSubview:avatarBtn];

        UIImageView *authImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_hairdress"]];
        [self addSubview:authImageView];
        authImageView.center = CGPointMake(avatarFrame.size.width+avatarFrame.origin.x-4, avatarFrame.size.height+avatarFrame.origin.y-4);
        
        CGFloat offset_x = avatarBtn.frame.size.width+avatarBtn.frame.origin.x + 6;
        
        nameLbl = [MSTool createLable:CGRectMake(offset_x,14,self.frame.size.width - offset_x,25)];
        [nameLbl setTextColor:[UIColor darkGrayColor]];
        [nameLbl setBackgroundColor:[UIColor clearColor]];
        [nameLbl setText:HYLocalizedString(@"")];
        [nameLbl setTextAlignment:UITextAlignmentLeft];
        [self addSubview:nameLbl];
        
        starsView = [[UIView alloc]initWithFrame:CGRectMake(nameLbl.frame.origin.x,nameLbl.frame.size.height+nameLbl.frame.origin.y - 4, nameLbl.frame.size.width, 30)];
        [self addSubview:starsView];
        
        UIImage *star = [UIImage imageNamed:@"me_star"];
        CGFloat star_offset_x = 0;
        for (int i = 0 ; i < 5; i++)
        {
            UIImageView *starImageView = [[UIImageView alloc]initWithImage:star];
            [starsView addSubview:starImageView];
            UIViewSetFrameX(starImageView, star_offset_x);
            star_offset_x+= star.size.width;
        }
        
    CGRect rect = CGRectMake(starsView.frame.origin.x, starsView.frame.size.height+starsView.frame.origin.y - 10, 30, 26);
    followBtn = [self createBtn:rect title:HYLocalizedString(@"关注")];
    [self addSubview:followBtn];
    
    fansBtn = [self createBtn:CGRectMake(followBtn.frame.size.width + followBtn.frame.origin.x + 4, followBtn.frame.origin.y, 30, 26) title:HYLocalizedString(@"粉丝")];
    [self addSubview:fansBtn];
    
    }
    return self;
}

-(void)refreshUI:(NSDictionary *)info
{
    HYLog(@"info--->%@",info);
    if (info && [info isKindOfClass:[NSDictionary  class]])
    {
        if ([info objectForKey:@"avatar"])
        {
            [avatarBtn setImageWithURL:[NSURL URLWithString:[info objectForKey:@"avatar"]] forState:UIControlStateNormal];
        }
        
        if ([info objectForKey:@"user_name"]) {
            nameLbl.text = [info objectForKey:@"user_name"];
        }
        
        CGFloat lbl_offset_x = 3;
        CGFloat btnWidth = 0;

        if ([info objectForKey:@"a_count"]) {
            UILabel *followlbl = (UILabel *)[followBtn viewWithTag:1];
            followlbl.text = [NSString stringWithFormat:@"%@",[info objectForKey:@"a_count"]];
            [followlbl sizeToFit];
            btnWidth = followlbl.frame.size.width+lbl_offset_x*2;
            if (btnWidth < 30)btnWidth = 30;
            UIViewSetFrameWidth(followBtn,btnWidth);
            UIViewSetFrameX(followlbl, btnWidth/2 - followlbl.frame.size.width/2);            
        }
        
        if ([info objectForKey:@"f_count"])
        {
            UILabel *fanslbl = (UILabel *)[fansBtn viewWithTag:1];
            fanslbl.text = [NSString stringWithFormat:@"%@",[info objectForKey:@"f_count"]];
            btnWidth = fanslbl.frame.size.width+lbl_offset_x*2;
            [fanslbl sizeToFit];
            if (btnWidth < 30)btnWidth = 30;
            UIViewSetFrameWidth(fansBtn,btnWidth);
            UIViewSetFrameX(fanslbl, btnWidth/2 - fanslbl.frame.size.width/2);
            UIViewSetFrameX(fansBtn, followBtn.frame.size.width + followBtn.frame.origin.x + 4);
        }
    }
}

-(UIButton *)createBtn:(CGRect )rect title:(NSString *)title
{
    UIImage *follow = [UIImage imageNamed:@"number_bg"];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setBackgroundImage:[follow stretchableImageWithLeftCapWidth:follow.size.width/2 topCapHeight:follow.size.height/2] forState:UIControlStateNormal];
    [btn setTitle:title forState:UIControlStateNormal];
    [btn setFrame:rect];
    [btn.titleLabel setFont:[UIFont systemFontOfSize:10]];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(12, 0, 0, 0)];
    [btn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [self addSubview:btn];
    
    UILabel *followCountLbl = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, btn.frame.size.width, 15)];
    [followCountLbl setFont:[UIFont systemFontOfSize:10]];
    [followCountLbl setText:@""];
    followCountLbl.tag = 1;
    [followCountLbl setTextAlignment:UITextAlignmentCenter];
    [followCountLbl setBackgroundColor:[UIColor clearColor]];
    [followCountLbl setTextColor:[UIColor colorWithString:@"#f376a0"]];
    [btn addSubview:followCountLbl];
    [followCountLbl sizeToFit];
    
    return btn;
}



/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

@end
